/*
 * web端埋点
 * version:1.0.1
 */
import FingerprintJS from "@fingerprintjs/fingerprintjs"
export default ({}, inject) => {
  const getRandom = (num) => {
    var random = Math.floor((Math.random() + Math.floor(Math.random() * 9 + 1)) * Math.pow(10, num - 1))
    return random
  }

  const myBrowser = (nav) => {
    var userAgent = nav.userAgent //取得浏览器的userAgent字符串
    var isOpera = userAgent.indexOf("Opera") > -1
    if (isOpera) {
      return "Opera"
    } //判断是否Opera浏览器
    if (userAgent.indexOf("Firefox") > -1) {
      return "FF"
    } //判断是否Firefox浏览器
    if (userAgent.indexOf("Chrome") > -1) {
      return "Chrome"
    }
    if (userAgent.indexOf("Safari") > -1) {
      return "Safari"
    } //判断是否Safari浏览器
    if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera) {
      return "IE"
    } //判断是否IE浏览器
  }

  const getvisitorId = async () => {
    const fp = await FingerprintJS.load()
    const result = await fp.get()
    const visitorId = result.visitorId
    return visitorId
  }

  const _optionPoint = {
    sendUrl: async (win, nav, doc) => {
      let visitorId = await getvisitorId()
      let bro = myBrowser(nav)
      return {
        br: bro,
        pm: bro,
        pr: win.screen.colorDepth || 0, //颜色深度
        ww: win.screen.width || 0, //屏幕宽度
        wh: win.screen.height || 0, //屏幕高度
        lang: nav.language || nav.browserLanguage || nav.systemLanguage || nav.userLanguage || "", //语言
        ev: "browse",
        wvv: "pcWebsite",
        wsdk: "2.23.4",
        pc: doc.location.href, //当前页面路径
        pdr: 0,
        at: new Date().getTime() + parseInt(Math.random() * 1000),
        v: "1.0.1",
        ifo: false,
        rq_c: 1,
        ls: getRandom(10), //十位随机数字
        te: "web",
        et: new Date().getTime(),
        st: new Date().getTime(),
        tp: 11000000,
        ct: encodeURIComponent(JSON.stringify({ title: doc.title })),
        op: visitorId //浏览器唯一标识
      }
    }
  }
  inject("optionPoint", _optionPoint)
}
